package net.dtdns.ldz.guessthenumber;

import java.util.Random;
import com.example.helloworld.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Random rand = new Random();
	private int numero;
	private boolean finished;

	private long score;

	private int numeroPossibilidades;
	private int numeroTentativas;

	private int numeroSeek;

	// Componentes
	private SeekBar seekbar;
	private TextView textViewScore;
	private TextView textViewNumero;
	private TextView textViewFinished;
	private ProgressBar progressBar;
	private Button checkButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.setTitle(R.string.title);
		textViewScore = (TextView) findViewById(R.id.lbScore);
		textViewFinished = (TextView) findViewById(R.id.textView2);
		textViewNumero = (TextView) findViewById(R.id.textView1);
		seekbar = (SeekBar) findViewById(R.id.seekBar1);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		checkButton = (Button) findViewById(R.id.btnGuess);

		start();
	}

	private void start() {
		finished = false;
		score = 0;

		numeroPossibilidades = 10;
		numeroTentativas = 0;
		seekbar.setMax(10);
		numeroSeek = 0;
		numero = rand.nextInt(numeroPossibilidades);
		progressBar.setMax(numeroPossibilidades);
		progressBar.setProgress(numeroPossibilidades);

		seekbar.setOnSeekBarChangeListener(new seekBarListener());
		textViewFinished.setText(getString(R.string.guessTheNumber));
		textViewScore.setText(getString(R.string.totalScore)
				+ String.valueOf(score));
	}

	private class seekBarListener implements SeekBar.OnSeekBarChangeListener {

		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			numeroSeek = progress;
			textViewNumero.setText(String.valueOf(numeroSeek));
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void restart() {
		finished = false;
		numeroTentativas = 0;
		numero = rand.nextInt(numeroPossibilidades);
		progressBar.setProgress(numeroPossibilidades);
		checkButton.setText(R.string.guessIt);
		seekbar.setVisibility(View.VISIBLE);
		textViewNumero.setVisibility(View.VISIBLE);
		textViewFinished.setText(getString(R.string.guessTheNumber));
		seekbar.setEnabled(true);
		seekbar.setProgress(0);
		numeroSeek = 0;
	}

	public void botaoGuessClick(View v) {
		if (finished) {
			restart();
		} else {
			numeroTentativas++;

			if (numeroSeek == numero) {
				int thisScore = ((numeroPossibilidades + 1) - numeroTentativas);
				score += thisScore;
				if (score < 0) {
					score = 0;
				}
				Toast.makeText(this, getString(R.string.guessed),
						Toast.LENGTH_SHORT).show();

				textViewScore.setText(getString(R.string.totalScore)
						+ String.valueOf(score));

				seekbar.setEnabled(false);
				seekbar.setVisibility(View.INVISIBLE);
				progressBar.setVisibility(View.VISIBLE);
				textViewNumero.setVisibility(View.INVISIBLE);
				textViewFinished.setText(getString(R.string.congratulations)
						+ "\n" + getString(R.string.score) + ": "
						+ String.valueOf(thisScore));
				checkButton.setText(getString(R.string.restart));
				finished = true;

			} else {
				progressBar
						.setProgress(numeroPossibilidades - numeroTentativas);

				Toast.makeText(
						this,
						getString(R.string.wrongAnswer)
								+ (numero > numeroSeek ? getString(R.string.bigger)
										: getString(R.string.lower)),
						(Toast.LENGTH_SHORT)).show();
			}
		}
	}
}
